package com.qiangesoft.jpa.dao;

import com.qiangesoft.jpa.entity.SysUser;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户持久层
 *
 * @author qiangesoft
 * @date 2024-10-12
 */
@Repository
public interface SysUserDao extends JpaRepository<SysUser, Long>, JpaSpecificationExecutor<SysUser> {

    /**
     * nativeQuery为true表示使用原生sql
     * @param userName
     * @return
     */
    @Query(value = "select * from sys_user where user_name =:userName", nativeQuery = true)
    SysUser findByUserName(@Param("userName") String userName);

    @Query(value = "select u from SysUser u where u.nickName like :nickName")
    Page<SysUser> pageByNickName(@Param("nickName") String nickName, Pageable pageable);

    @Modifying
    @Transactional
    @Query("update SysUser u set u.nickName =:nickName where u.id =:id")
    Integer updateById(@Param("nickName") String nickName, @Param("id") Long id);

}
